package com.lizhe.mybatisplus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lizhe.mybatisplus.model.AjaxResponse;
import com.lizhe.mybatisplus.model.Article;
import com.lizhe.mybatisplus.model.pageData;
import com.lizhe.mybatisplus.service.ArticleService;
import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 类描述: 增删改查
 *
 * @author Lz on 2021-10-28
 */
@RestController
@RequestMapping("/rest")
@Log
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping("/findOne")
    public AjaxResponse findOne(Integer id) {
        return AjaxResponse.success(articleService.getArticle(id));
    }

    @PostMapping("/save")
    public AjaxResponse save(@RequestBody Article article) {
        return AjaxResponse.success(articleService.insert(article));
    }


    @PostMapping("/list")
    public AjaxResponse list(Article article) {
        Page<Article> page = new Page<>(1, 10);
        articleService.findByAll(page, article);
        log.info("总条数" + page.getTotal());
        log.info("数据" + page.getRecords());
        pageData data = pageData.builder().data(page.getRecords())
                .current((int) page.getCurrent()).total((int) page.getTotal()).build();
        return AjaxResponse.success(data);
    }
}
